<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class GoodsdetController extends Controller
{
    public function index()
    {
    	$data = DB::table('goodsdet')->get();
    	// dd($data);
    	return view('admin.goodsdet.index',['data'=>$data]);
    }
    public function destroy($id)
    {
    	$m = DB::table('goodsdet')->where('id',$id)->delete();
    	if ($m>0) {
    		return redirect('/admin/goodsdet');
    	}else{
            return redirect('/admin/goodsdet');
        }
    }
    public function create()
    {
    	return view('admin.goodsdet.add');
    }
    public function store(Request $request)
    {
    	$list = DB::table('goodsdet')->get();
    	if ($request->hasFile('pic')) {
    		if ($request->file('pic')->isValid()) {
			    $file = $request->file('pic');
			    $ext = $file->getClientOriginalExtension();
			    $picname = rand(1,9999).'.'.$ext;
			    $file->move('./picppp/',$picname);
			}
		}
    	$messages = [
    		'required'=>':attribute是必须填的',
    	];
    	$this->validate($request,[
            'goodsdetid' => 'required',
    		],$messages);
		$data = array_filter($request->except('_token','MAX_FILE_SIZE'));
		$m = DB::table('goodsdet')->insertGetId($data);
		   if ($m>0) {
		    return redirect('/admin/goodsdet');
		   }else{
            return back();
           }
    }
    public function edit($id)
    {
    	$list = DB::table('goodsdet')->where('id',$id)->first();
        return view('admin.goodsdet.edit',['list'=>$list]);
    }
    public function update($id,Request $request)
    {
    	$data = array_filter($request->except('_token','_method'));
    	$m = DB::table('goodsdet')->where('id',$id)->update($data);
        if($m>0){
    		return redirect('/admin/goodsdet');
    	}else{
    		return redirect('/admin/goodsdet');
    	}
    }
}
